#!/bin/bash

set -e

ZM_REPO="https://zmrepo.zoneminder.com/debian/release-1.36"
DIST_CODENAME=$(cat /etc/os-release | grep VERSION_CODENAME | cut -d '=' -f 2)
DEPENDENCIES="apache2 apache2-utils php libapache2-mod-php php-mysql php-gd php-mysql mariadb-server apt-transport-https gnupg openssl"
INTERNAL_IP="$(hostname -I)"
INTERNAL_IP=${INTERNAL_IP%?}

function checkRoot()
{
        if [[ "${EUID}" -ne 0 ]]; then
                echo "You need to run this script as root"
                exit 1
        fi
}

function print_some_text()
{
        local NC=$(tput sgr0)
        local GREEN=$(tput setaf 2)
        echo "${GREEN}============================# ${1} #============================${NC}"
}


function do_install()
{
        print_some_text "Updating system"
        bash -c "apt update && apt update"

        print_some_text "Installing dependencies"
        bash -c "apt install -y ${DEPENDENCIES}"

        print_some_text 'Answer "yes" to everything'
        bash -c "mysql_secure_installation"

        print_some_text "Adding ZoneMinder repository"
        bash -c "wget -O - https://zmrepo.zoneminder.com/debian/archive-keyring.gpg | apt-key add -"
        bash -c "echo 'deb ${ZM_REPO} ${DIST_CODENAME}/' > /etc/apt/sources.list.d/zoneminder.list"
        bash -c "apt update"

        print_some_text "Installing ZoneMinder"
        bash -c "apt install -y zoneminder"

        print_some_text "Starting ZoneMinder"
        bash -c "chmod 740 /etc/zm/zm.conf"
        bash -c "chown root:www-data /etc/zm/zm.conf"
        bash -c "chown -R www-data:www-data /usr/share/zoneminder/"
        bash -c "adduser www-data video"
        bash -c "a2enmod rewrite cgi expires headers"
        bash -c "a2enconf zoneminder"
        bash -c "systemctl enable apache2 mariadb zoneminder"
        bash -c "systemctl start apache2"
        bash -c "systemctl start zoneminder"
        bash -c "systemctl reload apache2"
        print_some_text "Intallation done"
}

set_up_https()
{
        read -p "Set up https? (y/n): " yesno
        case $yesno in
                [Yy]* )
                        print_some_text "Generating ssl certificates"
                        if [[ ! -d /etc/apache2/certs ]]; then
                                bash -c "mkdir /etc/apache2/certs"
                        fi
                        bash -c "openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out /etc/apache2/certs/cert.crt -keyout /etc/apache2/certs/key.key"

                        print_some_text "Updating apache configuration"
                bash -c "echo \
        '<Directory /var/www/html>
                AllowOverride All
        </Directory>' >> /etc/apache2/apache2.conf"

                bash -c "echo \
        '<VirtualHost *:80>
                RewriteEngine On
                RewriteCond %{HTTPS} !=on
                RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
        </VirtualHost>
        <VirtualHost *:443>
                ServerAdmin webmaster@localhost
                DocumentRoot /var/www/html
                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined
                SSLEngine on
                SSLCertificateFile /etc/apache2/certs/cert.crt
                SSLCertificateKeyFile /etc/apache2/certs/key.key
        </VirtualHost>' > /etc/apache2/sites-enabled/000-default.conf "

                        bash -c "a2enmod ssl"
                        bash -c "a2enmod rewrite"
                        bash -c "systemctl restart zoneminder"
                        bash -c "systemctl restart apache2"
                        ;;
        esac
}

checkRoot
do_install
set_up_https
echo "Go to ${INTERNAL_IP}/zm in your web browser to continue configuration"
